package com.travelsky.airchina.boarding.util;

public class PageUtil {

	private int curPage;

	private int endnum;

	private int maxResult;

	private boolean needPaging;

	private int perPageRow;

	private int startnum;

	private int totalPage;

	private int totalRow;

	public PageUtil() {
		iniat();
	}
	
	public int getCurPage() {
		return this.curPage;
	}

	public int getEndnum() {
		return this.endnum;
	}

	public int getFirstPage() {
		return 1;
	}

	public int getMaxResult() {
		return this.maxResult;
	}

	public int getNextPage() {
		if (curPage == totalPage) {
			return totalPage;
		}
		return this.curPage + 1;
	}

	public int getPerPageRow() {
		return this.perPageRow;
	}

	public int getPrePage() {
		if (curPage == 1) {
			return curPage;
		}
		return this.curPage - 1;
	}

	public int getStartnum() {
		return this.startnum;
	}

	public int getTotalPage() {
		return this.totalPage;
	}

	public int getTotalRow() {
		return this.totalRow;
	}

	public void iniat() {
		curPage = 1;
		startnum = 1;
		endnum = 10;
		perPageRow = 10;
		totalPage = 1;
		totalRow = 0;
		needPaging = true;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
		if (this.curPage < 1) {
			this.curPage = 1;
		}
	}

	public void setEndnum(int endnum) {
		this.endnum = endnum;
		if (this.endnum < 1) {
			this.endnum = 1;
		}
	}

	public boolean isNeedPaging() {
		return needPaging;
	}

	public void setNeedPaging(boolean needPaging) {
		this.needPaging = needPaging;
	}

	public void setMaxResult(int maxResult) {
		this.maxResult = maxResult;
		if (this.maxResult < 1) {
			this.maxResult = 1;
		}
	}

	public void setPerPageRow(int perPageRow) {
		//if (perPageRow > 0)
			this.perPageRow = perPageRow;
	}

	public void setStartnum(int startnum) {
		this.startnum = startnum;
		if (this.startnum < 1) {
			this.startnum = 1;
		}
	}

	public void setTotalPage(int totalPage) {
		if (totalPage > 0)
			this.totalPage = totalPage;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
		if (this.totalRow < 0) {
			this.totalRow = 0;
		}
	}

	public void sumPage() {
		if (needPaging)
			if (perPageRow < 1)
				perPageRow = 1;
		if (totalRow > 0) {
			totalPage = (int) ((totalRow - 1) / perPageRow) + 1;
			startnum = perPageRow * (curPage - 1) + 1;
			if (curPage * perPageRow < totalRow) {
				endnum = curPage * perPageRow; 
				maxResult = perPageRow;
			} else {
				endnum = totalRow;
				maxResult = totalRow - startnum + 1;
			}
		} else {
			totalRow = 0;
			totalPage =1;
			curPage = 1;
			startnum = 1;
			endnum = 10;
		}
	}
}
